# class Person:
#     # 类属性
#     species = "Human"
 
#     # 初始化方法（构造方法）
#     def __init__(self, name, age):
#         self.name = name  # 实例属性
#         self.age = age    # 实例属性
 
#     # 实例方法
#     def greet(self):
#         print(f"Hello, my name is {self.name} and I am {self.age} years old.")

class Person:
    # 类属性
    species = "Human"

    # 初始化方法（构造方法）
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    # 实例方法
    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# 创建对象
person1 = Person("Alice", 25)
 
# 访问属性
print(person1.name)  # 输出：Alice
print(person1.age)   # 输出：25
 
# 调用方法
person1.greet()  # 输出：Hello, my name is Alice and I am 25 years old.


class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
 
dog1 = Dog("Buddy", "Golden Retriever")
dog2 = Dog("Max", "German Shepherd")
 
print(dog1.name)  # 输出：Buddy
print(dog2.breed) # 输出：German Shepherd


class Dog:
    species = "Canine"  # 类属性
 
dog1 = Dog()
dog2 = Dog()
 
print(dog1.species)  # 输出：Canine
print(dog2.species)  # 输出：Canine


class Circle:
    def __init__(self, radius):
        self.radius = radius
 
    def area(self):
        return 3.14 * self.radius ** 2
 
circle = Circle(5)
print(circle.area())  # 输出：78.53981633974483


class Dog:
    species = "Canine"
 
    @classmethod
    def set_species(cls, new_species):
        cls.species = new_species
 
Dog.set_species("Wolf")
print(Dog.species)  # 输出：Wolf


# class Math:
#     @staticmethod
#     def add(a, b):
#         return a + b
 
# print(Math.add(5, 3))  # 输出：8

class Math:
    @staticmethod
    def add(a, b):
        return a + b
 
print(Math.add(5, 3))  # 输出：8


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
person = Person("Alice", 25)
print(person.name)  # 输出：Alice


class Person:
    def __del__(self):
        print("对象被销毁")
 
person = Person()
del person  # 输出：对象被销毁


class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # 私有属性
 
    def deposit(self, amount):
        self.__balance += amount
 
    def get_balance(self):
        return self.__balance
 
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())  # 输出：1500

# 1. 创建实例，触发 __init__ 方法
account = BankAccount(1000)  # self.__balance = 1000

# 2. 调用 deposit 方法修改 __balance
account.deposit(500)        # self.__balance += 500 → 1500

# 3. 通过 get_balance 获取值
print(account.get_balance()) # 返回 self.__balance → 1500


class Animal:
    def __init__(self, name):
        self.name = name
 
    def speak(self):
        print(f"{self.name} makes a sound.")
 
class Dog(Animal):  # Dog 继承 Animal
    def speak(self):
        print(f"{self.name} barks.")
 
dog = Dog("Buddy")
dog.speak()  # 输出：Buddy barks.


class Animal:
    def speak(self):
        print("Animal makes a sound.")
 
class Dog(Animal):
    def speak(self):
        print("Dog barks.")
 
class Cat(Animal):
    def speak(self):
        print("Cat meows.")
 
animals = [Dog(), Cat(), Animal()]
for animal in animals:
    animal.speak()
# 输出：
# Dog barks.
# Cat meows.
# Animal makes a sound.



class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    def __str__(self):
        return f"{self.name}, {self.age} years old"
 
person = Person("Alice", 30)
print(person)  # 输出：Alice, 30 years old



class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)
 
    def __str__(self):
        return f"({self.x}, {self.y})"
 
p1 = Point(1, 2)
p2 = Point(3, 4)
print(p1 + p2)  # 输出：(4, 6)


class A:
    def say(self):
        print("A says Hello")
 
class B:
    def say(self):
        print("B says Hello")
 
class C(A, B):
    pass
 
c = C()
c.say()  # 输出：A says Hello（根据继承顺序）


from abc import ABC, abstractmethod
 
class Shape(ABC):
    @abstractmethod
    def area(self):
        pass
 
class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height
 
    def area(self):
        return self.width * self.height
 
rect = Rectangle(5, 3)
print(rect.area())  # 输出：15